

import java.util.ArrayList;
import java.util.List;

/**
 * @auther zzc
 * @date 2021/3/31 16:24
 */
public class 二叉树中的中序遍历 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /**
     * 方法一   递归   O(N) O(N)
     *
     * @param treeNode
     * @return
     */
    //中序遍历treeNode
    public List<Integer> sortList(TreeNode treeNode) {
        List<Integer> res = new ArrayList<>();
        getResult(treeNode, res);
        return res;
    }

    private void getResult(TreeNode treeNode, List<Integer> res) {
        //如果树节点为空  则退出遍历
        if (treeNode == null) {
            return;
        }
        //遍历当前节点的左边节点  递归
        getResult(treeNode.left, res);
        //把当前节点值存进list
        res.add(treeNode.val);
        //遍历右边节点  递归
        getResult(treeNode.right, res);
    }

    /**
     * 方法二  栈     O(N) O(N)
     */

}
